package com.mach.platform.sprider.dev;



import com.mach.platform.sprider.Conf;
import com.mach.platform.util.SqlHelper;

import java.io.IOException;

/*
    LinkKeWork S7244
 */
public class LeegDev extends DataHelper {

    protected String devName;

    protected SqlHelper sqls;

    public LeegDev(String ip, Integer port, Integer devID, String devName) {
        super(ip,port,devID);

        this.sqls  = Conf.getSqlHelper();

        this.devName = devName;
    }

    public static void main(String[] args) throws IOException {
        findAll(args);
    }
    //扫描参数
    public static void findAll(String[] args) throws IOException {
        int busNumStartIx = 1,busNumEndIx = 10; //总线地址开始与结束参数
        int tcpPortStartIx = 30001,tcpPortEndIx = 30001; //TCP端口开始与结束参数
        String ip = "192.168.1.200";
        for(int j = tcpPortStartIx; j <= tcpPortEndIx; j+=4){
            for(int i = busNumStartIx; i <= busNumEndIx; ++i){
                long st = System.currentTimeMillis();
               readIdById(ip,j,i,"XXXX" );
               long dt = System.currentTimeMillis() - st;
               System.out.println("dt="+ dt);
           }
       }
    }
    public static void setId(String[] args) throws IOException {
        LeegDev dev =  new LeegDev("192.168.1.200",30028,2,"XXXX" );
        byte[] data = dev.readSth(10, 1, 3);
        UU.printData(data);
        byte[] data2 = dev.setDataRtu(10,5);
        UU.printData(data2);
    }
    public static void readIdById(String ip ,Integer port, Integer id,String devName) throws IOException {
        LeegDev dev =  new LeegDev(ip,port,id,devName);
        byte[] data = dev.readSth(4, 2, 3);
        //U.printData(data);

    }

    public byte[] readSth(Integer addr, Integer num, Integer cmdCode){
        byte[] result = null;


        byte[] repData = new byte[0];
        try {
            repData = this .getDataRtu(addr,num);
//            if(repData.length <1)return null;
//            byte[] data = ByteUtil.subData(repData,3,4);
//            data = ByteUtil.reverse(data);
            result = repData;
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }



    public byte[] getDataRtu(Integer startAddr, Integer len) throws IOException {
        IntNum startAddrNum = new IntNum(startAddr);
        IntNum lenNUmum = new IntNum(len);

        byte[] result = {};

        byte[] sendData = new byte[6];

        sendData[0] = this.devID.byteValue();
        sendData[1] = 3;
        sendData[2] = startAddrNum.h2();
        sendData[3] = startAddrNum.l2();
        sendData[4] = lenNUmum.h2();
        sendData[5] = lenNUmum.l2();

        byte[] sendDataHasCRC = UU.setCrc16Btye(sendData);

        System.out.print("devId:" + this.getDevAddr() + " <-- ");//debug log
        UU.printData(sendDataHasCRC);
        //		this.wk.send(sendData);
//		result = this.wk.read4TimeOut();

        result = this.wk.command(sendDataHasCRC);

        System.out.print("devId:" + this.getDevAddr() + " --> bytes>");//debug log
        UU.printData(result);


        return result;
    }
    public byte[] setDataRtu(Integer startAddr, Integer value) throws IOException {
        IntNum startAddrNum = new IntNum(startAddr);
        IntNum valueNUmum = new IntNum(value);

        byte[] result = {};

        byte[] sendData = new byte[6];

        sendData[0] = this.devID.byteValue();
        sendData[1] = 6;
        sendData[2] = startAddrNum.h2();
        sendData[3] = startAddrNum.l2();
        sendData[4] = valueNUmum.h2();
        sendData[5] = valueNUmum.l2();

        byte[] sendDataHasCRC = UU.setCrc16Btye(sendData);

        System.out.print("devId:" + this.getDevAddr() + " <-- ");//debug log
        UU.printData(sendDataHasCRC);
        //		this.wk.send(sendData);
//		result = this.wk.read4TimeOut();

        result = this.wk.command(sendDataHasCRC);

        System.out.print("devId:" + this.getDevAddr() + " --> bytes>");//debug log
        UU.printData(result);


        return result;
    }

    @Override
    public void run(){

//        long preSec = 0;
//        long nowSec = 0;
//        while(true){
//
//
//            try {
//
//                DateTime dateTime = new DateTime();
//                int fyear = dateTime.getYear();
//                int fmonth = dateTime.getMonthOfYear();
//                int fday = dateTime.getDayOfMonth();
//                int fhour = dateTime.getHourOfDay();
//                int fmin = dateTime.getMinuteOfHour();
//                int fsec = dateTime.getSecondOfMinute();
//
//                Float volt =  this.getVolt();
//                Float elect = this.getElect();
//                Float power = this.getPower();
//                Double enery = this.getEnery();
//
//                try {
//                    this.sqls.updateAsync("insert ignore into data_detail(dev_id, fyear, fmonth, fday, fhour, fmin, fsec,fdatetime, " +
//                            "volt,elect,power,enery) value" +
//                            "('"+this.devName+"',"+fyear+","+fmonth+","+fday+","+fhour+","+fmin+","+fsec+","+"date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
//                            "'%Y-%m-%d %H:%i:%s'),"
//                            +volt+","+elect+","+power+","+enery+")");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//
//                try {
//                    this.sqls.updateAsync("replace into data_cur (dev_id,volt,elect,power,update_time) value " +
//                            "( " +
//                            "'"+this.devName+"',"+
//                            volt + ","+
//                            elect + ","+
//                            power + "," +
//                            "date_format(concat("+fyear+",'-',"+fmonth+",'-',"+fday+",' ',"+fhour+",':',"+fmin+",':',"+fsec+")," +
//                                    "'%Y-%m-%d %H:%i:%s')" +
//                            ")");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
////                Thread.sleep(1000*60);
//
//
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            preSec = nowSec;
//
//
//        }


    }

    /*
      取变频器:电压
   */
    public Float getVolt() {
        Float result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40006,4);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,4);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getFloat(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }



    /*
    取变频器:能量
 */
    public Double getEnery() {
        Double result = null;

        DataHelper dh = this;

        if(dh == null)return null;

        byte[] repData = new byte[0];
        try {
            repData = dh.getDataRtu(40266,8);
            if(repData.length <1)return null;
            byte[] data = ByteUtil.subData(repData,3,8);
            data = ByteUtil.reverse(data);
            result = ByteUtil.getDouble(data);
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("excep devId:" + this.devName + " " + e.getMessage());
        }


        return result;
    }

}
